﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace GameNesSL.Views
{
    using Controls;
    using nesemu;

    public partial class Settings : Page
    {
        public Settings()
        {
            InitializeComponent();
        }

        private GameFieldConfig cfg = null;
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            cfg = Cache.GameCache.GetConfig();

            p1A.KeySet = (Key)cfg.Player1.A;
            p1B.KeySet = (Key)cfg.Player1.B;
            p1Up.KeySet = (Key)cfg.Player1.Up;
            p1Down.KeySet = (Key)cfg.Player1.Down;
            p1Left.KeySet = (Key)cfg.Player1.Left;
            p1Right.KeySet = (Key)cfg.Player1.Right;

            p2A.KeySet = (Key)cfg.Player2.A;
            p2B.KeySet = (Key)cfg.Player2.B;
            p2Up.KeySet = (Key)cfg.Player2.Up;
            p2Down.KeySet = (Key)cfg.Player2.Down;
            p2Left.KeySet = (Key)cfg.Player2.Left;
            p2Right.KeySet = (Key)cfg.Player2.Right;

            select.KeySet = (Key)cfg.Player1.Select;
            start.KeySet = (Key)cfg.Player1.Start;

            load.KeySet = cfg.QuickLoad;
            save.KeySet = cfg.QuickSave;

        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            Cache.GameCache.SetConfig(cfg);
        }

        private void settings_BackClick(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/StartPage", UriKind.Relative));
        }

        private void P1KeySelected(JoyButton button, Key key)
        {
            switch (button)
            {
                case JoyButton.A:
                    cfg.Player1.A = (int) key;
                    break;
                case JoyButton.B:
                    cfg.Player1.B = (int)key;
                    break;
                case JoyButton.Left:
                    cfg.Player1.Left = (int)key;
                    break;
                case JoyButton.Right:
                    cfg.Player1.Right = (int)key;
                    break;
                case JoyButton.Up:
                    cfg.Player1.Up = (int)key;
                    break;
                case JoyButton.Down:
                    cfg.Player1.Down = (int)key;
                    break;
            }
            Cache.GameCache.SetConfig(cfg);
        }

        private void P2KeySelected(JoyButton button, Key key)
        {
            switch (button)
            {
                case JoyButton.A:
                    cfg.Player2.A = (int)key;
                    break;
                case JoyButton.B:
                    cfg.Player2.B = (int)key;
                    break;
                case JoyButton.Left:
                    cfg.Player2.Left = (int)key;
                    break;
                case JoyButton.Right:
                    cfg.Player2.Right = (int)key;
                    break;
                case JoyButton.Up:
                    cfg.Player2.Up = (int)key;
                    break;
                case JoyButton.Down:
                    cfg.Player2.Down = (int)key;
                    break;
            }
            Cache.GameCache.SetConfig(cfg);
        }

        private void CommonKeySelected(JoyButton button, Key key)
        {
            switch (button)
            {
                case JoyButton.Select:
                    cfg.Player1.Select = (int)key;
                    break;
                case JoyButton.Start:
                    cfg.Player1.Start = (int)key;
                    break;
                case JoyButton.Save:
                    cfg.QuickSave = key;
                    break;
                case JoyButton.Load:
                    cfg.QuickLoad = key;
                    break;
            }
            Cache.GameCache.SetConfig(cfg);
        }
    }
}
